#!/bin/bash

# Set up MESOS_HOME in order to find projd
export MESOS_HOME=/root/mesos

# Set MESOS_PUBLIC_DNS so slaves can be linked in master web UI
export MESOS_PUBLIC_DNS=`wget -q -O - http://instance-data.ec2.internal/latest/meta-data/public-hostname`

# Set PATH to include Scala
export PATH=$PATH:/root/scala-2.9.0.1/bin

# Set HADOOP_HOME variable to allow slaves to get executors from HDFS
export HADOOP_HOME=/root/persistent-hdfs

ulimit -n 8192

PROGRAM=$1
shift

EXTRA_OPTS=""
if [ "$PROGRAM" == "mesos-slave" ]; then
  # Compute CPU and memory resources, if not specified.
  if [[ "$*" != *--resources* ]]; then
    CPUS=`grep processor /proc/cpuinfo | wc -l`
    MEM_KB=`cat /proc/meminfo | grep MemTotal | awk '{print $2}'`
    MEM=$[(MEM_KB - 1024 * 1024) / 1024]
    EXTRA_OPTS="--resources=cpus:$CPUS;mem:$MEM"
  fi
fi

if [ "$PROGRAM" == "mesos-master" ]; then
  # Don't keep failed schedulers around waiting for them to reconnect
  EXTRA_OPTS+=" --failover-timeout=0"
fi

cd $MESOS_HOME/bin
nohup ./$PROGRAM --log_dir=/mnt/mesos-logs --work_dir=/mnt/mesos-work "$EXTRA_OPTS" "$@" </dev/null >/mnt/mesos-logs/$PROGRAM.out 2>&1 &
